import baseApi from '@/config/server/api/base'
import userApi from '@/config/server/api/user';

function processList() {
  return baseApi.processManage.processList.fetch().then(res => {
    return res
  })
}

function processSave(params) {
  return baseApi.processManage.processSave.fetch(params).then(res => {
    return res
  })
}

function processUpdate(params) {
  return baseApi.processManage.processUpdate.fetch(params).then(res => {
    return res
  })
}
//流程详情分页列表
function detailList(params) {
  return baseApi.processManage.detailList.fetch(params).then(res => {
    return res
  })
}
//流程详情保存
function detailSave(params) {
  return baseApi.processManage.detailSave.fetch(params).then(res => {
    return res
  })
}
//流程管理修改
function detailUpdate(params) {
  return baseApi.processManage.detailUpdate.fetch(params).then(res => {
    return res
  })
}
//流程详情删除
function detailDelete(data) {
  return baseApi.processManage.detailDelete.fetch(data).then(res => {
    return res
  })
}

//查询公司所有角色
function getAllRoleNameList() {
  return userApi.role.roleNameList.fetch().then(res => {
    return res
  })
}

//查询业务组织架构
function getBusinessOrganTree() {
  return userApi.organ.getWebOrg.fetch().then(res => {
    let newArr = multistageArr(res)
    newArr[0].expand = true // 默认展开第一项
    return newArr
  })
}

// 处理tree
function multistageArr(arr) {
  let newArr = []
  arr.forEach(item => {
    // if (item.level == '2') {
    //   item.children = []
    // }
    item.title = item.orgName
    item.id = item.orgId
    if (item.children && item.children.length) {
      multistageArr(item.children); //递归遍历
    }
    newArr.push(item)
  })
  return newArr
}

export default {
  processList,
  processSave,
  processUpdate,
  detailList,
  detailSave,
  detailUpdate,
  detailDelete,
  getBusinessOrganTree,
  getAllRoleNameList
}